package com.softer.wxzj.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.softer.wxzj.common.entity.CommonResult;
import com.softer.wxzj.common.entity.Page;
import com.softer.wxzj.common.enums.ShiroResultEnum;
import com.softer.wxzj.entity.FlowType;
import com.softer.wxzj.mapper.FlowConfigMapper;
import com.softer.wxzj.mapper.FlowTypeMapper;
import com.softer.wxzj.service.FlowTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(rollbackFor = { RuntimeException.class, Exception.class })
public class FlowTypeServiceImpl implements FlowTypeService {

    @Autowired
    private FlowTypeMapper flowTypeMapper;
    @Autowired
    private FlowConfigMapper flowConfigMapper;

    @Override
    public CommonResult insert(FlowType flowType) {
        //类型不可重复
        int count = flowTypeMapper.getCount(flowType.getFlowType());
        if(count>0){
            return new CommonResult(ShiroResultEnum.FAIL.getCode(), "流程类型已存在");
        }
        int i = flowTypeMapper.insert(flowType);
        return CommonResult.ResponseResult(i);
    }

    @Override
    public CommonResult update(FlowType flowType) {
        int i = flowTypeMapper.update(flowType);
        return CommonResult.ResponseResult(i);
    }

    @Override
    public CommonResult delete(String id) {
        //包含流程的类型不可删
        FlowType flowType = (FlowType)flowTypeMapper.getInfo(id);
        if(null != flowType){
            int count = flowConfigMapper.getCountByFlowType(flowType.getFlowType());
            if(count>0){
                return new CommonResult(ShiroResultEnum.FAIL.getCode(), "流程类型已被使用");
            }
        }
        int i = flowTypeMapper.delete(id);
        return CommonResult.ResponseResult(i);
    }

    @Override
    public PageInfo<FlowType> getList(Page page, FlowType flowType) {
        //只有紧跟在PageHelper.startPage方法后的第一个Mybatis的查询（Select）方法会被分页。
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<FlowType> list = flowTypeMapper.getList(flowType);
        PageInfo<FlowType> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public FlowType getInfo(String id) {
        return flowTypeMapper.getInfo(id);
    }


}
